﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using MyBlog.Model;
using MyBlog.IService;
using Utility;
using Microsoft.AspNetCore.Authorization;
using AutoMapper;
using MyBlog.Model.DTO;

namespace MyBlog.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]//使用JWT鉴权特性,控制器上面加,所有的Action都会带上 
    public class WriterInfoController : ControllerBase
    {
        private readonly IWriterInfoService _iWriterInfoService;
        public WriterInfoController(IWriterInfoService IWriterInfoService)
        {
            this._iWriterInfoService = IWriterInfoService;
        }

        //[HttpGet("WriterInfos")]
        //public async Task<ApiResult> WriterInfos()
        //{
        //    var WriterInfos = await _IWriterInfoService.QueryAsync();
        //    if (WriterInfos.Count == 0)
        //        return ApiResultHelper.Error("没有更多的类型");
        //    return ApiResultHelper.Success(WriterInfos);
        //}

        [HttpPost("Create")]
        public async Task<ApiResult> Create(string name, string username, string userpwd)
        {
            if (string.IsNullOrWhiteSpace(name))
                return ApiResultHelper.Error("类型名不能为空");
            WriterInfo writerInfo = new WriterInfo
            {
                Name = name,
                UserPwd = MD5Helper.MD5Encrypt32(userpwd),
                UserName = username,
            };
            //判断数据库中是否存在账号跟要添加的账号相同的数据
            var oldWriter = await _iWriterInfoService.FindAsync(c => c.UserName == username);
            if (oldWriter != null)
            {
                return ApiResultHelper.Error("账号已经存在");
            }
            bool b = await _iWriterInfoService.CreateAsync(writerInfo);
            if (!b)
                return ApiResultHelper.Error("添加失败");
            return ApiResultHelper.Success(writerInfo);//为什么不是b呢?
        }

        [HttpPut("Edit")]
        public async Task<ApiResult> Edit(string name)
        {
            int id = Convert.ToInt32(this.User.FindFirst("Id").Value);
            
            var writer = await _iWriterInfoService.FindAsync(id); 
            writer.Name = name;
            bool b = await _iWriterInfoService.EditAsync(writer); 
            if (!b) 
                return ApiResultHelper.Error("修改失败"); 
            return ApiResultHelper.Success("修改成功");  
        }

#if true 
        //此访问会令前端不直接返回密码
        [AllowAnonymous]//允许匿名
        [HttpGet("Findwriter")]
        public async Task<ApiResult> Findwriter([FromServices] IMapper iMapper,int id)
        {
            var writer = await _iWriterInfoService.FindAsync(id);
            var writerDTO = iMapper.Map<WriterDTO>(writer);
            return ApiResultHelper.Success(writerDTO);
        }
#else
        //此访问会令前端直接返回密码
        [AllowAnonymous]//允许匿名
        [HttpGet("Findwriter")]
        public async Task<ApiResult> Findwriter(int id)
        {
            var writer = await _iWriterInfoService.FindAsync(id);
            return ApiResultHelper.Success(writer);
        } 
#endif



    }
}
